package com.xywy.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.xywy.common.util.MLog;

public class NetWorkReceiver extends BroadcastReceiver {
    String TAG = "NetWorkReceiver";
    private NetWorkListener listener;

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE); // 网络链接服务
        NetworkInfo mobileInfo = manager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); // 移动链接

        NetworkInfo wifiInfo = manager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI); // wifi链接
        NetworkInfo activeInfo = manager.getActiveNetworkInfo(); // 可用的链接
        if (activeInfo != null && wifiInfo != null && mobileInfo != null) {
            MLog.i(TAG,
                    "mobile:" + mobileInfo.isConnected() + "\n" + "wifi:"
                            + wifiInfo.isConnected() + "\n" + "active:"
                            + activeInfo.getTypeName()
            );
        } else if (activeInfo == null && wifiInfo != null && mobileInfo != null) {
            MLog.i(TAG,
                    "mobile:" + mobileInfo.isConnected() + "\n" + "wifi:"
                            + wifiInfo.isConnected() + "\n"
            );
        }
        if (listener != null) {
         /*   if (NetUtil.hasNetwork(context)) {
                listener.hasNetwork();
            } else {
                listener.noNetwork();
            }*/
        }
    }

    public void setNetWorkListener(NetWorkListener listener) {
        this.listener = listener;
    }

    /**
     * 网络状态监听器
     */
    public interface NetWorkListener {
        /**
         * 网络状态改变为有网络时调用
         */
        public void hasNetwork();

        /**
         * 网络状态改变为无网络时调用
         */
        public void noNetwork();
    }

}
